﻿using IGF.Utils;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

namespace LikeProxy.Controls.RequestBodyType
{
    public partial class FormUrlencodedControl : UserControl
    {
        public FormUrlencodedControl()
        {
            InitializeComponent();
            this.Dock = DockStyle.Fill;
            rBtn_set_body_list.Checked = true;

            dataGV_req_body.Height = 600;
        }

        public Dictionary<string, string> GetRequestBody()
        {
            Dictionary<string, string> body = new Dictionary<string, string>();
            if (rBtn_set_body_text.Checked)
            {
                foreach (var item in textBox_req_body.Text.Split('&'))
                {
                    var keyValue = item.Split('=');
                    if (keyValue.Length == 2)
                    {
                        body.Add(keyValue[0].Trim(), keyValue[1].Trim());
                    }
                }
            }
            else
            {
                foreach (DataGridViewRow item in dataGV_req_body.Rows)
                {
                    if (item.Cells[1].Value == null || item.Cells[2].Value == null)
                    {
                        continue;
                    }
                    bool has = (bool)item.Cells[0].Value;
                    string key = (string)item.Cells[1].Value;
                    string value = (string)item.Cells[2].Value;
                    if (has == true && key != "")
                    {
                        body.Add(key, value);
                    }
                }
            }
            return body;
        }

        private void RadioButtonCheckedChanged(object sender, EventArgs e)
        {
            RadioButton btn = sender as RadioButton;
            if (btn.Checked == true)
            {
                textBox_req_body.Visible = rBtn_set_body_text.Checked;
                dataGV_req_body.Visible = rBtn_set_body_list.Checked;

                if (btn.Name == "rBtn_set_body_list")
                {
                    dataGV_req_body.Location = new Point(5, 33);
                    dataGV_req_body.Rows.Clear();
                    foreach (var item in textBox_req_body.Text.Split('&'))
                    {
                        var keyValue = item.Split('=');
                        if (keyValue.Length == 2)
                        {
                            dataGV_req_body.AddRow(new object[]
                            {
                                true,
                                keyValue[0],
                                keyValue[1],
                            });
                        }
                    }

                }
                else if (btn.Name == "rBtn_set_body_text")
                {
                    List<string> datas = new List<string>();
                    foreach (DataGridViewRow item in dataGV_req_body.Rows)
                    {
                        if (item.Cells[1].Value == null || item.Cells[2].Value == null)
                        {
                            continue;
                        }
                        bool has = (bool)item.Cells[0].Value;
                        string key = (string)item.Cells[1].Value;
                        string value = (string)item.Cells[2].Value;
                        if (has == true && key != "")
                        {
                            datas.Add($"{key}={value}");
                        }
                    }
                    textBox_req_body.Text = string.Join("&", datas.ToArray());
                    textBox_req_body.Location = new Point(5, 33);
                }
            }
        }

    }
}
